package net.flydy.ywxcx.wxcloudrun.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class URLParser {
    public static Map<String, String> parseQuery(String query, String charset) {
        // 解码查询字符串
        String decodedQuery = null;
        try {
            decodedQuery = URLDecoder.decode(query, charset);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        // 获取参数
        Map<String, String> params = new HashMap<>();
        Pattern pattern = Pattern.compile("([^&=]+)=([^&=]*)");
        Matcher matcher = pattern.matcher(decodedQuery);
        while (matcher.find()) {
            params.put(matcher.group(1), matcher.group(2));
        }
        return params;
    }
}